package com.freexiaoyu.cloud.ui.popupwindow;


import android.app.Activity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;


import com.freexiaoyu.cloud.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * Created by DIY on 2018/3/16. 14:24
 * author:free宇
 * email:freexiaoyu@foxmail.com
 * describe:
 */

public class CommonPopupWindow extends BasePopupWindow {
    AppCompatTextView tv_title;
    LinearLayoutCompat ll_layout;

    private View popupView;
    protected IOnClickListener onClickListener;
    HashMap<String, String> hashMap;
    String strTile;
    int index = 0;

    public CommonPopupWindow(Activity context, HashMap<String, String> hashMap, String strTile) {
        super(context);
        this.hashMap = hashMap;
        this.strTile = strTile;
        bindEvent();
        setData();
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(
                R.layout.popup_select_item, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return null;
    }

    @Override
    protected Animation getShowAnimation() {
        return null;
    }

    @Override
    protected View getClickToDismissView() {
        return null;
    }

    private void bindEvent() {
        if (popupView != null) {
            tv_title.setText(strTile);
            tv_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }


    private void setData() {
        Iterator iter = hashMap.entrySet().iterator();
        while (iter.hasNext()) {
            final Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            final Object val = entry.getValue();
            View mView = LayoutInflater.from(mContext).inflate(R.layout.view_popup_common_item_layout, null);
            AppCompatTextView tv_title =mView.findViewById(R.id.tv_title);
            tv_title.setText(String.valueOf(val));
            mView.setTag(key);
            mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListener != null) {
                        onClickListener.OnClickListener(v, Integer.valueOf(v.getTag()+""), val);
                    }
                }
            });
            ll_layout.addView(mView);
            index++;
        }
    }

    public interface IOnClickListener {
        void OnClickListener(View view, int index, Object obj);
    }

    /***
     * 点击事件方法
     *
     * @param OnClickListener
     */
    public void setOnClickListener(IOnClickListener OnClickListener) {
        this.onClickListener = OnClickListener;
    }
}